<?
/**
 * VoiceOne Settings/System/IO
 *
 * Frontend to provide access to the IO system in voiceone. It allows to 
 * turn on/off pbx service and to turn off or reboot the pc.  
 * @package voiceone
 * @subpackage  settings_system_io
 */
 
  include ("../../../admin/config/cfg.php");
  global $cwlang, $cwLanguage;
  require_once("$absBasePath/lib/xajax/xajax.inc.php");
  
  //================ XAJAX ================
  function rollingAndDo($target, $action){
    global $relBasePath, $cwlang;
    // Instantiate the xajaxResponse object
    $objResponse = new xajaxResponse();
    // Build response
    $html="<table><tr>";
    $html.=" <td><img src=\"".$relBasePath."public/img/ico-progress.gif"."\" alt=\"running\" /></td>";
    //PBX ACTION
    if($target=="pbx"){
      if($action=="start") $html.=" <td><p>".$cwlang["io"]["starting"]."</p></td>";
      else if ($action=="stop") $html.=" <td><p>".$cwlang["io"]["stopping"]."</p></td>";
      $html.=" </tr></table>";
      $objResponse->addAssign("pbxrolling","innerHTML", $html);
      $newimg=" document.getElementById('tlpbx').src=\"".$relBasePath."/public/img/tl-yellow.gif\"; ";
      $objResponse->addScript($newimg);
    }
    //SYSTEM ACTION
    else if($target=="system"){
      if($action=="halt") $html.=" <td><p>".$cwlang["io"]["shutingDown"]."</p></td>";
      else if ($action=="reboot") $html.=" <td><p>".$cwlang["io"]["rebooting"]."</p></td>";
      $html.=" </tr></table>";
      $objResponse->addAssign("systemrolling","innerHTML", $html);
      $newimg=" document.getElementById('tlsystem').src=\"".$relBasePath."/public/img/tl-yellow.gif\"; ";
      $objResponse->addScript($newimg);
    }
    $script=" xajax_action('".$target."','".$action."') ";
    $objResponse->addScript($script);
    // Return the xajaxResponse object
    return $objResponse->getXML();  
  }
  function action($target, $action){
    global $relBasePath, $asterisk;
    // Instantiate the xajaxResponse object
    $objResponse = new xajaxResponse();
    // Build response
    $ret="";
    if($target=="pbx"){
      if ($action=="start") $ret=$asterisk->pbxSwitch("start");  
      else $ret=$asterisk->pbxSwitch("stop"); 
      if($ret=="ON"){
        //modify I/O page
        $newimg=" document.getElementById('tlpbx').src=\"".$relBasePath."/public/img/tl-green.gif\"; ";
        $objResponse->addScript($newimg);
        $objResponse->addAssign("send","disabled","");
        //modify template
        $tplimg=" document.getElementById('tplpbxsytatus').src=\"".$relBasePath."/public/img/tl-green-small.gif\"; ";
        $objResponse->addScript($tplimg);
        //reset rolling
        $objResponse->addAssign("pbxrolling","innerHTML", ""); 
      }else if($ret=="OFF"){
        //modify I/O page
        $newimg=" document.getElementById('tlpbx').src=\"".$relBasePath."/public/img/tl-red.gif\"; ";
        $objResponse->addScript($newimg);
        $objResponse->addAssign("send","disabled","disabled");
        //modify template
        $tplimg=" document.getElementById('tplpbxsytatus').src=\"".$relBasePath."/public/img/tl-red-small.gif\"; ";
        $objResponse->addScript($tplimg);
        //reset rolling
        $objResponse->addAssign("pbxrolling","innerHTML", ""); 
      }else{
        $newimg=" document.getElementById('tlpbx').src=\"".$relBasePath."/public/img/tl-yellow.gif\"; ";
        $objResponse->addScript($newimg);
        $objResponse->addAssign("pbxrolling","innerHTML", "<p color=\"#FF0000\"> ERROR </p>");
      }
    }
    else if($target=="system"){
      if($action=="halt"){
        $ret=$asterisk->shutdown("halt");
        if($ret>0){
          $newimg=" document.getElementById('tlsystem').src=\"".$relBasePath."/public/img/tl-red.gif\"; ";
          $objResponse->addScript($newimg);
          $objResponse->addAssign("systemrolling","innerHTML", "");
        }else{
          $newimg=" document.getElementById('tlsystem').src=\"".$relBasePath."/public/img/tl-yellow.gif\"; ";
          $objResponse->addScript($newimg);
          $objResponse->addAssign("systemrolling","innerHTML", "<p color=\"#FF0000\"> ERROR </p>");
        }
      }else if($action=="reboot"){
        $ret=$asterisk->shutdown("reboot");
        if($ret>0){
          $newimg=" document.getElementById('tlsystem').src=\"".$relBasePath."/public/img/tl-red.gif\"; ";
          $objResponse->addScript($newimg);
          $objResponse->addAssign("systemrolling","innerHTML", "");
        }else{
          $newimg=" document.getElementById('tlsystem').src=\"".$relBasePath."/public/img/tl-yellow.gif\"; ";
          $objResponse->addScript($newimg);
          $objResponse->addAssign("systemrolling","innerHTML", "<p color=\"#FF0000\"> ERROR </p>");
        }
      }else{
        $newimg=" document.getElementById('tlsystem').src=\"".$relBasePath."/public/img/tl-yellow.gif\"; ";
        $objResponse->addScript($newimg);
        $objResponse->addAssign("systemrolling","innerHTML", "<p color=\"#FF0000\"> ERROR </p>");
      }
    }
    // Return the xajaxResponse object
    return $objResponse->getXML();  
  }
  $xajax = new xajax();
  $xajax->registerFunction("rollingAndDo");
  $xajax->registerFunction("action");
  $xajax->processRequests();
  
  //================= PAGE LOGIC ==================
  // set pbx traffic light
  $status=$asterisk->pbxSwitch("status");
  if($status=="ON") $pbximg="<img id=\"tlpbx\" src=\"".$relBasePath."/public/img/tl-green.gif\">";
  else if($status=="OFF") $pbximg="<img id=\"tlpbx\" src=\"".$relBasePath."/public/img/tl-red.gif\">";
  else $pbximg="<img id=\"tlpbx\" src=\"".$relBasePath."/public/img/tl-yellow.gif\">";
  // set system traffic light
  $systemimg="<img id=\"tlsystem\" src=\"".$relBasePath."/public/img/tl-green.gif\">";

  //================ MAIN CONTENTS ================
  ob_start();
  $xajax->printJavascript("../../../admin/lib/xajax/"); ?>
  
   <?$sel="io";include("../navigation.php");?> 
  
  <div class="SectionHeader">
    <?=$alert?>
    <h1><?=$cwlang["io"]["title"]?></h1>
  </div>

  <div id="basic_form">
    <table>
      <tr>
        <!-- PBX -->
        <td style="width: 50%">
          <h2><?=$cwlang["io"]["pbx"]?></h2>
          <p class="blockintro"><?=$cwlang["io"]["pbxdesc"]?></p>
          <div class="block">
            <table class="block">
              <tr>
                <td>
                  <table style="width: 10%">
                    <tr>
                      <td rowspan="2"><?=$pbximg?></td>
                      <td><a href="#"><img src="<?=$relBasePath?>/public/img/shutdown.png" onclick="if (confirm('<?=$cwlang['io']['pbxstop']?>')) xajax_rollingAndDo('pbx','stop');" ></a></td><td><a href="#" onclick="if (confirm('<?=$cwlang['io']['pbxstop']?>')) xajax_rollingAndDo('pbx','stop');"><?=$cwlang["io"]["stop"]?></a></td>
                    </tr>
                    <tr>
                      <td><a href="#"><img src="<?=$relBasePath?>/public/img/ok.gif" onclick="if (confirm('<?=$cwlang['io']['pbxstart']?>')) xajax_rollingAndDo('pbx','start'); else return false;" ></a></td><td><a href="#" onclick="if (confirm('<?=$cwlang['io']['pbxstart']?>')) xajax_rollingAndDo('pbx','start');"><?=$cwlang["io"]["start"]?></a></td>
                    </tr>
                  </table>
                </td>
                <td id="pbxrolling"></td>
              </tr>
            </table>
          </div>
        </td>
        <!-- System -->
        <td style="width: 50%">
          <h2><?=$cwlang["io"]["system"]?></h2>
          <p class="blockintro"><?=$cwlang["io"]["systemdesc"]?></p>
          <div class="block">
            <table class="block">
              <tr>
                <td>
                  <table style="width: 10%">
                    <tr>
                      <td rowspan="2"><?=$systemimg?></td>
                      <td><a href="#"><img src="<?=$relBasePath?>/public/img/shutdown.png" onclick="if (confirm('<?=$cwlang['io']['systemturnoff']?>')) xajax_rollingAndDo('system','halt');" ></a></td><td><a href="#" onclick="if (confirm('<?=$cwlang['io']['systemturnoff']?>')) xajax_rollingAndDo('system','halt');"><?=$cwlang["io"]["shutDown"]?></a></td>
                    </tr>
                    <tr>
                      <td><a href="#"><img src="<?=$relBasePath?>/public/img/reboot.png" onclick="if (confirm('<?=$cwlang['io']['systemreboot']?>')) xajax_rollingAndDo('system','reboot');" ></a></td><td><a href="#" onclick="if (confirm('<?=$cwlang['io']['systemreboot']?>')) xajax_rollingAndDo('system','reboot');"><?=$cwlang["io"]["reboot"]?></a></td>
                    </tr>
                  </table>
                </td>
                <td id="systemrolling"></td>
              </tr>
            </table>
          </div>
        </td>        
      </tr>
    </table>
  </div>
  <?
  $contents=ob_get_contents();
  ob_end_clean();

  //================ SIDEBAR ================
  ob_start(); 
  ?>
    <div class="Sidebar">
      <h1><?=$cwlang["io"]["sidebarTitle"]?></h1>
          <?=$cwlang["io"]["sidebarDesc"]?>
      <table id="trafficlight" cellspacing="1">
        <tr>
          <td><img align="middle" src="<?=$relBasePath?>/public/img/tl-red.gif"></td>
          <td><img src="<?=$relBasePath?>/public/img/tl-yellow.gif"></td>
          <td><img src="<?=$relBasePath?>/public/img/tl-green.gif"></td>
        </tr>
        <tr>
          <th>OFF</th>
          <th>Changing</th>
          <th>ON</th>
        </tr>
      </table>  
    </div>    
           
  <?  
  $sidebar=ob_get_contents();
  ob_end_clean();

  //================ SETTINGS ================

  $smarty->assign("section", "system");
  $smarty->assign("subsection", "");
  $smarty->assign("templateType", "TwoColLeft");  //OneCol, TwoColLeft, TwoColRight
  $smarty->assign("contents", $contents);
  $smarty->assign("sidebar", $sidebar);
  $smarty->display('settings.tpl');
?>

